/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.ui;

import com.matsuzine.utils.HelperMethods;
import com.sudolink.crypto.CryptoException;
import com.sudolink.cryptodata.CryptInfoBundle;
import com.sudolink.cryptodata.SecureDataManager;
import com.sudolink.io.Fetch;
import com.sudolink.io.Store;
import com.sudolink.isp.app.Application;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * The GateKeeper is the initial UI of the application. It provides the user
 * with a login screen and the option to create a new database.
 * 
 * @author Matthew MacGregor
 */
public class GateKeeperManager {

    public void chooseDBPath() {
        
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.showSaveDialog(null);
        File f = jfc.getSelectedFile();
        if (f != null) {
            Application.setPathToDatabase(f.getAbsolutePath());
        }
        
    }

    public boolean processLogin(char[] pw1) {
        
        boolean isValidLogin = false;
        try {
            
            CryptInfoBundle bundle = new CryptInfoBundle();
            bundle.setPassword(HelperMethods.convertCharArrayToByteArray(pw1));
            String pathToDatabase = Application.getPathToDatabase();
            final File db = new File(Application.getPathToDatabase());
            if (db.exists() == false) {
                int result = JOptionPane.showConfirmDialog(null, "This Database does not exist. Click yes to create a new database.");
                if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.NO_OPTION) {
                    return false;
                }

                bundle.setSalt(HelperMethods.generateSalt());
            }

            Application.setPrivateKeys(bundle);
            SecureDataManager sdm = new SecureDataManager();
            Application.setSecureDataManager(sdm);
            
            if (new File(Application.getPathToDatabase()).exists()) {
                sdm.loadAll(new Fetch(db), bundle );
            } else {
                sdm.saveAll(new Store(pathToDatabase), bundle );
            }

            MainWindow mw = new MainWindow();
            mw.setLocationRelativeTo(null);
            mw.setVisible(true);
            isValidLogin = true;
            
        } catch (IOException ex) {
            Logger.getLogger(GateKeeperManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CryptoException ex) {
            Logger.getLogger(GateKeeperManager.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Your password isn't right.");
        }

        return isValidLogin;
    }
}
